﻿using System;
using System.Collections.Generic;
using System.Linq;

using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons;

#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif


namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_ButtonArea:ControlBase
    {
        private List<ButtonItemBase> _lst=new List<ButtonItemBase>(2);

        public PageForm_ButtonArea(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-btn-area");
        }

        public PageForm_ButtonArea AddButton(Action<ButtonItemBase> configFunc)
        {
            var btn=new ButtonItemBase();
            btn.Class("weui-btn");

            configFunc(btn);

            _lst.Add(btn);
            
            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine($"<div  {_attrs}>");

            foreach (var item in _lst)
            {
                item.Class(Utility.ButtonStyleToStr(item.getButtonStyle));
                //item.Attributes.MergeAttr("onclick", item._clickScript);

                var href = item.getType == ButtonTypeEnum.Link ? $"href=\"{item.getHref}\"" : "";
                var clickScript = item.getType != ButtonTypeEnum.Link && !string.IsNullOrWhiteSpace(item.getOnClickScript) ? $"onclick=\"{item.getOnClickScript}\"" : "";
                var tag = item.getType == ButtonTypeEnum.Link ? "a" : "button";

                if (item.getType!=ButtonTypeEnum.Link)
                {
                    item.Attributes.MergeAttr("type", item.getType == ButtonTypeEnum.Button ? "button" : "submit");
                }

                ViewContext.Writer.WriteLine($"<{tag} {item.Attributes} {href} {clickScript} >{item.getText}</{tag}>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end  btn-area-->");
            return MvcHtmlString.Empty;
        }
    }
}